﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AliyunOpenSearch4Net;

namespace AliyunOpenSearch4Net.Base
{
    class CommonQueryInfo
    {
        private static readonly Random _random = new Random();

        public CommonQueryInfo()
        {
            Version = "v2";
            SignatureMethod = "HMAC-SHA1";
            Timestamp = DateTime.Now;
            SignatureVersion = "1.0";
            SignatureNonce = DateTime.Now.Subtract(DateTime.MinValue).TotalMilliseconds.ToString() + _random.Next(1000, 9999).ToString();
        }

        public CommonQueryInfo(string accessKeyId)
            : this()
        {
            AccessKeyId = accessKeyId;
        }

        public string Version { get; set; }

        public string AccessKeyId { get; set; }

        public string Signature { get; set; }

        public string SignatureMethod { get; set; }

        public DateTime Timestamp { get; private set; }

        public string SignatureVersion { get; set; }

        public string SignatureNonce { get; private set; }

        public void Register(IList<KeyValuePair<string, string>> queryItems)
        {
            queryItems.Add("Version", Version);
            queryItems.Add("AccessKeyId", AccessKeyId);
            queryItems.Add("SignatureMethod", SignatureMethod);
            queryItems.Add("SignatureVersion", SignatureVersion);
            queryItems.Add("SignatureNonce", SignatureNonce);
            queryItems.Add("Timestamp", Timestamp.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ"));
        }
    }
}
